<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>黑马电商后台管理系统</span>
      </div>
      <el-button type="infor" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="iscollapse ? 64px:200px">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 默认高亮 default active,让他变成动态的值 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
        >
          <!-- 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
          <!-- 这里的index只接受字符串 -->
          <el-submenu :index="item.id+''" :key="item.id" v-for="item in MenuList">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index=" '/'+item.path"
              :key="item.id"
              v-for="item in item.children"
              @click="saveNavState('/'+item.path)"
            >
              <i class="el-icon-menu"></i>
              {{item.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      MenuList: [],
      iconObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      iscollapse: false,
      activePath: ""
    };
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    // 获取左侧列表数据
    async getMenuList() {
      const res = await this.$http.get("/menus");
      if (res.meta.status !== 200) return;
      this.MenuList = res.data;
    },
    toggleCollapse() {
      this.iscollapse = !this.iscollapse;
    },
    saveNavState(activePath) {
      // window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  },
  mounted() {
    // 获取菜单列表
    this.getMenuList();
    // this.activePath = window.sessionStorage.getItem("activePath");
    // 判断是否访问的是welcome页面
    this.activePath = this.$route.path == "/welcome" ? "" : this.$route.path;
    this.activePath =
      this.$route.path == "/goods/add" ? "/goods" : this.$route.path;
  },
  watch: {
    // 监测路由变化，to是去往的地址，from是来自哪里
    "$route.path": function(to, from) {
      if (to == "/goods/add") return (this.activePath = "/goods");
      this.activePath = to;
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      img {
        margin-right: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
  .iconfont {
    margin-right: 10px;
  }
  .el-menu {
    border-right: 0;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
</style>